home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
program
/
funnel.zoo
/
sources
/
analyse.c
next >
Wrap
C/C++ Source or Header
|
1993-04-11
|
12KB
|
297 lines
/*##############################################################################
FUNNNELWEB COPYRIGHT
====================
FunnelWeb is a literate-programming macro preprocessor.
Copyright (C) 1992 Ross N. Williams.
Ross N. Williams
ross@spam.adelaide.edu.au
16 Lerwick Avenue, Hazelwood Park 5066, Australia.
This program is free software; you can redistribute it and/or modify
it under the terms of Version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See Version 2 of the GNU General Public License for more details.
You should have received a copy of Version 2 of the GNU General Public
License along with this program. If not, you can FTP the license from
prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Section 2a of the license requires that all changes to this file be
recorded prominently in this file. Please record all changes here.
Programmers:
RNW Ross N. Williams ross@spam.adelaide.edu.au
Changes:
07-May-1992 RNW Program prepared for release under GNU GPL V2.
##############################################################################*/
/******************************************************************************/
/* ANALYSE.C */
/******************************************************************************/
#include "style.h"
#include "analyse.h"
#include "as.h"
#include "data.h"
#include "lister.h"
#include "misc.h"
#include "table.h"
/******************************************************************************/
LOCAL void chk_mac P_((p_ma_t));
LOCAL void chk_mac (p_ma)
p_ma_t p_ma;
{
mc_t *call;
/* Deal with undefined macros by flagging the first invocation. */
if (!p_ma->ma_defn.md_isdef)
{
ls_loo(p_ma->ma_calls,(ulong) 1,PPV &call);
as_cold(call!=NULL,"check_macro: No instances of \"undefined\" macro!");
lr_err(&call->mc_ps,"Call to undefined macro.");
return;
}
/* Assert: The macro has a definition. */
/* Check that all the calls to the macro have the correct number of params. */
ls_fir(p_ma->ma_calls);
ls_nxt(p_ma->ma_calls,PPV &call);
while (call!=NULL)
{
if (call->mc_npar != p_ma->ma_defn.md_npar)
{
sprintf(linet1,"Call has %lu parameter%s, but the macro",
(ulong) call->mc_npar,
call->mc_npar==1 ? "" : "s");
lr_err(&(call->mc_ps),linet1);
sprintf(linet1,"definition at line %lu specifies %lu parameter%s.",
(ulong) p_ma->ma_defn.md_ps.ps_line,
(ulong) p_ma->ma_defn.md_npar,
p_ma->ma_defn.md_npar==1 ? "" : "s");
lr_mes(&(call->mc_ps),linet1);
if (p_ma->ma_defn.md_npar==0 && call->mc_npar==1)
{
lr_mes(&(call->mc_ps),"Note: If your call looks like this: @<Macro@>@(@) then you");
lr_mes(&(call->mc_ps),"should be aware that FunnelWeb treats this as a call with");
lr_mes(&(call->mc_ps),"a single parameter consisting of the empty string. This");
lr_mes(&(call->mc_ps),"is a consequence of parameter delimiters @\"@\" being optional.");
}
}
ls_nxt(p_ma->ma_calls,PPV &call);
}
/* Check that the number of calls of the macro is legal. */
if (p_ma->ma_defn.md_isfil)
{
/* Flag all calls to a file macro as errors. */
if (ls_len(p_ma->ma_calls)>0)
{
lr_mes(&p_ma->ma_defn.md_ps,
"This file macro is called one or more times.");
lr_mes(&p_ma->ma_defn.md_ps,
"Each call has been flagged with an error message.");
}
ls_fir(p_ma->ma_calls);
ls_nxt(p_ma->ma_calls,PPV &call);
while (call!=NULL)
{
lr_err(&(call->mc_ps),"Calls to file macros are not allowed.");
lr_mes(&(call->mc_ps),"Reason: It should be possible to comment out a");
lr_mes(&(call->mc_ps),"file macro without a big fuss. Calling a file macro");
lr_mes(&(call->mc_ps),"just tangles it up in the macro structure,");
lr_mes(&(call->mc_ps),"making it more difficult to comment out later.");
ls_nxt(p_ma->ma_calls,PPV &call);
}
}
else
{
/* Non-file macros. */
if (ls_len(p_ma->ma_calls)==0 && !p_ma->ma_defn.md_iszer)
lr_err(&p_ma->ma_defn.md_ps,
"This macro is never used (and has no @Z).");
if (ls_len(p_ma->ma_calls)>1 && !p_ma->ma_defn.md_isman)
lr_err(&p_ma->ma_defn.md_ps,
"This macro is used more than once (and has no @M).");
}
}
/******************************************************************************/
LOCAL void chk_rec P_((void));
LOCAL void chk_rec()
/* This macro analyses the calls between the macros in the macro */
/* table and flags all recursive macros with an error message. */
/* */
/* Def: A macro is a "level 0 macro" if it does not call any macros. */
/* Def: A macro is a "level n macro" if it calls at least one macro of level */
/* n-1, but does not call any macros of level n or higher. */
/* Def: A macro is a "level infinity macro" if it directly or indirectly */
/* calls itself. */
/* */
/* This function totally ignores undefined macros. This means that it can be */
/* invoked even if problems are detected by the other checks. */
/* */
/* Although the actions of this may seem rather time expensive, they may not */
/* be. The function repeatedly runs through the macro table and each macro's */
/* bodypart list and expression lists. However, there are not likely to be */
/* many macros and each definition expression list is likely to be quite */
/* short, as each macro will be mainly text. */
/* Even if this stuff does take a while, it is better to detect recursion now */
/* than at tangle time! */
{
p_ma_t p_ma; /* General purpose pointer to macro. */
name_t dummy_name; /* Dummy name variable acts as a placeholder in calls. */
uword level; /* Level of macro currently being tagged. */
bool gotone; /* TRUE iff current loop iteration tagged >0 macros. */
/* We need a concrete number to stand for level infinity. */
/* 30000 is a nice, large 15 bit number. */
#define LEVINF 30000
/* Make sure that our idea of infinity is big enough. */
as_cold(tb_len(macro_table)<LEVINF,"ana_rec: too many macros.");
/* Tag all the macros as being at level infinity. */
/* This could be done in the parser, but it's neater to do it here. */
tb_fir(macro_table);
while (tb_rea(macro_table,PV dummy_name,PV &p_ma))
if (p_ma->ma_defn.md_isdef)
p_ma->ma_level=LEVINF;
gotone=TRUE;
/* Work from level 0 upwards identifying macros of successive levels. */
for (level=0; level<LEVINF && gotone; level++)
{
/* Identify all macros of level 'level'. */
/* Gotone tells us if we managed to tag a macro during this iteration. */
gotone=FALSE;
/* Run through table tagging all macros at level 'level'. */
tb_fir(macro_table);
while (tb_rea(macro_table,PV dummy_name,PV &p_ma))
{
if (p_ma->ma_defn.md_isdef && p_ma->ma_level == LEVINF)
{
p_bpls_t p_bpls = p_ma->ma_defn.md_body;
/* Run through each body part of the macro. */
ls_fir(p_bpls);
while (TRUE)